<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="org.wso2.carbon.registry.common.ui.UIException" %>
<%@ page import="org.wso2.carbon.registry.custom.skb.ui.utils.AddKBUtil" %>
<%@ page import="org.wso2.carbon.registry.custom.skb.ui.clients.TopicServiceClient" %>
<%@ page import="java.util.ArrayList"  %>

<%
//    String parentPath = request.getParameter("parentPath");
    String parentPath = "/SupportKB/Problem";
    String problemName = request.getParameter("problemName");
    String sPath = "/SupportKB/Problem/"+problemName;
//    String sPath = parentPath +"/"+ problemName;

//    String problemContent="<problem></pro>";

    String description = request.getParameter("description");
    String link = request.getParameter("link");
    String question = request.getParameter("qnArray");
    System.out.println("Q[] :" +question);
    String qn[] = question.split(",");
    String answer = request.getParameter("ansArray");
    System.out.println("A[] :" +answer);
    String ans[] = answer.split(",");
    String solution = request.getParameter("solArray");
    System.out.println("SOL[] :" +solution);
    String sol[] = solution.split(",");
    String product = request.getParameter("product");
    System.out.println("Product :"+product);
    String productRelease = request.getParameter("productRelease");
    System.out.println("PR :"+productRelease);
    String carbonPlatform = request.getParameter("carbonPlatform");
    System.out.println("CP :"+carbonPlatform);
    String jdkPlatform = request.getParameter("jdkPlatform");
    System.out.println("JDK :"+jdkPlatform);

    String keyWord = request.getParameter("keyWord");

    String problemContent = "<problem problemName=\""+problemName+"\" description=\""+description+"\" link=\""+link+"\"><questions question=\""+question+"\" /><answers answer=\""+answer+"\" /><solutions solution=\""+answer+"\" /><products product=\""+product+"\" productRelease=\""+productRelease+"\" /><platform carbonPlatform=\""+carbonPlatform+"\" jdkPlatform=\""+jdkPlatform+"\" /><keyWords keyWord=\""+keyWord+"\" /></problem>";

   
    try {
        AddKBUtil.addKBItem(request, config, session);
        TopicServiceClient client = new TopicServiceClient(config, session);
        client.addProblemResource("/SupportKB/Problem/"+problemName, problemContent);
        client.addAssociation(sPath, link, "Issue Link");
       
        String problemPath = "/SupportKB/Problem";
        String solPath = "/SupportKB/Solution";
        String aPath = "/SupportKB/Answer";
        String qPath = "/SupportKB/Question";
        client.addCollection("/SupportKB");
        client.addCollection(problemPath);
        client.addCollection(qPath);
        client.addCollection(aPath);
        client.addCollection(solPath);

        
        
//        client.addProblemResource("/SupportKB/Problem/"+problemName, list(problemName,description,link,question,answer,solution,product,productRelease,carbonPlatform,jdkPlatform,keyWord) );
//        client.addCollection("/WSO2Products/Product");
//        client.addCollection("/WSO2Products/Product_Release");
        for(int i=0;i<qn.length;i++){
            if(qn[i]!=null){
            client.addResource("/SupportKB/Question/"+qn[i], qn[i]);
            client.addAssociation(sPath, "/SupportKB/Question/"+qn[i], "Question : "+(i+1));
            }
        }
        for(int i=0;i<ans.length;i++){
            if(ans[i]!=null){
            client.addResource("/SupportKB/Answer/"+ans[i], ans[i]);
            client.addAssociation(sPath, "/SupportKB/Answer/"+ans[i], "Answer : "+(i+1));
            }
        }
        for(int i=0;i<sol.length;i++){
            if(sol[i]!=null){
            client.addResource("/SupportKB/Solution/"+sol[i], sol[i]);
            client.addAssociation(sPath, "/SupportKB/Solution/"+sol[i], "Solution : "+(i+1));
            }
        }
        client.addCollection("/WSO2Products/"+product+"/Product_Release");
        client.addResource("/WSO2Products/"+product+"/Product_Release/"+productRelease, productRelease);
        client.addAssociation(sPath, "/WSO2Products/"+product, "Product");
        client.addAssociation(sPath, "/WSO2Products/"+product+"/Product_Release/"+productRelease, "ProductRelease");

        client.addCollection("/Platform/Carbon_Platform");
        client.addCollection("/Platform/JDK_Platform");

        client.addResource("/Platform/Carbon_Platform/"+carbonPlatform ,carbonPlatform);
        client.addAssociation(sPath, "/Platform/Carbon_Platform/"+carbonPlatform, "CarbonPlatform");
        client.addResource("/Platform/JDK_Platform/"+jdkPlatform, jdkPlatform);
        client.addAssociation(sPath, "/Platform/JDK_Platform/"+jdkPlatform, "JDKPlatform");

//        System.out.println("Asso Added");
//        client.addKeyWord(sPath, "test tag");
        client.addKeyWord(sPath, keyWord);
//        System.out.println("KeyWord Added");

    } catch (UIException e) {
        %>
Could not add endpoint
<%
        return;
    }

    String resourcePagePath = "../../../resources/resource.jsp?path=" + parentPath;

//    String resourcePagePath = "/carbon/registry/custom/skb/skb_view_ajaxprocessor.jsp";
    response.sendRedirect(resourcePagePath);
%>